package com.goods.business.converter;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.vo.business.OutStockVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class OutStockConverter {

  @Autowired
  private ConsumerMapper consumerMapper;

  public List<OutStockVO> converterToVOList(List<OutStock> outStocks){
    List<OutStockVO> outStockVOS = new ArrayList<>();
    if (!CollectionUtils.isEmpty(outStocks)){
      for (OutStock outStock : outStocks) {
        OutStockVO outStockVO = new OutStockVO();
        BeanUtils.copyProperties(outStock,outStockVO);
        Consumer consumer = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
        if (consumer!=null){
          outStockVO.setName(consumer.getName());
          outStockVO.setPhone(consumer.getPhone());
        }
        outStockVOS.add(outStockVO);
      }
    }
    return outStockVOS;
  }
}
